define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/product/index',
                    add_url: 'merchant/product/add',
                    edit_url: 'merchant/product/edit',
                    del_url: 'merchant/product/del',
                    multi_url: 'merchant/product/multi',
                    table: 'product',
                }
            });

            var table = $("#table");
            table.on('click', '.approve', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/product/approve?id=' + id, '操作', {
                    area: ['380px', '300px']
                });
            });
            table.on('click', '.refuse', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/product/refuse?id=' + id, '操作', {
                    area: ['560px', '380px']
                });
            });
            table.on('click', '.info', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/product/info?id=' + id, '详情', {

                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'cid', title: '产品分类', searchList: productClassifyOptions, formatter: function(value, data, index) {
                            return data.product_classify;
                        }},
                        {field: 'P.name', title: '商品名称', operate: 'LIKE', formatter: function(value, data, index) {
                            return data.name;
                        }}, 
                        {field: 'selling', title: '卖点', operate: 'LIKE'},
                        {field: 'photo_url', title: '产品主图', events: Table.api.events.image, formatter: function(value, data, index){
                            let row = Table.api.formatter.image(value, data, index);
                            return row;
                        }, operate: false},
                        {field: 'photos_url', title: '产品图片', events: Table.api.events.image, formatter: function(value, data, index){
                            let row = Table.api.formatter.image(value, data, index);
                            return row;
                        }, operate: false},
                        {field: 'S.name', title: '店铺', formatter: function(value, row, index) {
                            return row.store_name;
                        }, operate: 'LIKE'},
                        {field: 'P.status', title: '产品状态', searchList: productStatusOptions, formatter: function(value, data, index) {
                            return data.status_text;
                        }},
                        {field: 'P.examine', title: '审核状态', searchList: productExamineOptions, formatter: function(value, data, index) {
                            return data.examine_text;
                        }},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '更新时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'info', title: '详情', formatter: function(value, data, index) {
                            return '<button class="btn btn-info info" data-id="' + data.id + '">详情</button>';
                        }, operate: false},
                        {field: '', title: '操作', formatter: function(value, data, index) {
                            if(data.examine == 0) {
                                return '<button id="approve" class="btn btn-info approve" data-id="' + data.id + '">通过</button>' + '&nbsp;&nbsp;' + '<button id="refuse" class="btn btn-info refuse" data-id="' + data.id + '">拒绝</button>';
                            }
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        odr: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/product/odr',
                    edit_url: 'merchant/product/odr_edit',
                    multi_url: 'merchant/product/multi',
                    table: 'product',
                }
            });

            var table = $("#table");

            table.on('click', '.odr', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/product/odr_edit?id=' + id, '操作', {
                });
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'name', title: '商品名称'}, 
                        {field: 'photo_url', title: '产品主图', events: Table.api.events.image, formatter: function(value, data, index){
                            let row = Table.api.formatter.image(value, data, index);
                            return row;
                        }, operate: false},
                        {field: 'odr', title: '产品权重'},
                        {field: '', title: '操作', formatter: function(value, data, index) {
                            return '<button id="odr" class="btn btn-info odr" data-id="' + data.id + '">修改权重</button>';
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        odr_edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});